﻿using DSPL.Contracts.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace DSPL.Contracts.Converters
{
    public class FilterEquationToBoolean : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((string)value == "is empty (blank)" || (string)value == "is not empty" || (string)value == "is true" || (string)value == "is false")
            {
                return false;
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }

    public class EnumToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) return false;
            string enumValue = value.ToString();
            string targetValue = parameter.ToString();
            bool outputValue = enumValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
            return outputValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null) return null;
            bool useValue = (bool)value;
            string targetValue = parameter.ToString();
            if (useValue) return Enum.Parse(targetType, targetValue);
            return null;
        }
    }

    public class BooleanToNumericConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != DBNull.Value)
            {
                if (System.Convert.ToInt32(value) == 1)
                {
                    return true;
                }
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (System.Convert.ToBoolean(value) == true)
            {
                return 1;
            }
            else
            {
                return 0;
            }

        }
    }

    public class FormModeToBoolean : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((Modes)(value) == Modes.New)
            {
                return true;
            }

            if ((Modes)(value) == Modes.Edit && (parameter.ToString().Trim() != "N"))
            {
                return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //if ((System.Convert.ToBoolean(value) && System.Convert.ToString(parameter).Equals("Y"))
            //    || (!System.Convert.ToBoolean(value) && System.Convert.ToString(parameter).Equals("N")))
            //{
            //    return 'Y';
            //}
            //else
            //{
            //    return 'N';
            //}
            return true;
        }
    }

    public class NumericValueToBooleanConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value is int)
            {
                var val = (int)value;
                return (val == 0) ? false : true;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            if (value != null && value is bool)
            {
                var val = (bool)value;
                return val ? 1 : 0;
            }
            return null;
        }
    }


    public class StringToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value.ToString().Trim() == string.Empty) && (parameter.ToString().Trim() == "N"))
                return true;
            else if ((value.ToString().Trim() == string.Empty) && (parameter.ToString().Trim() == "Y"))
                return false;
            else
            return value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(true) ? parameter : Binding.DoNothing;
        }

    }


    public class NumericToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != DBNull.Value && System.Convert.ToInt32(value) == (System.Convert.ToInt32(parameter)))
            //if (value !=DBNull.Value &&  System.Convert.ToInt32(value)==1 && (System.Convert.ToInt32(parameter) == 1))
                return true;

            //else if (value != DBNull.Value && System.Convert.ToInt32(value) == 0 && (System.Convert.ToInt32(parameter) == 0))
            //{
            //    return true;
            //}

            else if (value == DBNull.Value && System.Convert.ToInt32(parameter) == 0)
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(true) ? parameter : Binding.DoNothing;
        }

    }

    //public class ColomnDataTypeToStringConverter : IValueConverter
    //{

    //    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    //    {
    //        string returnValue;
    //        switch (parameter)
    //        {
    //            case "INT16":
    //                    case "INT32":
    //                    case "INT64":
    //                    case "DECIMAL":
    //                        control.textBox.Text = e.NewValue.ToString();

    //                        break;
    //                    case "DATETIME":
    //                        //control.datePicker.SelectedDate = DateTime.Parse(e.NewValue.ToString());
    //                        break;

    //                    default:
    //                        break;
    //        }
    //        return returnValue;
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    //    {           
    //        throw new NotImplementedException();
    //    }
    //}


    public class CodeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != DBNull.Value && System.Convert.ToInt32(value) == (System.Convert.ToInt32(parameter)))
                //if (value !=DBNull.Value &&  System.Convert.ToInt32(value)==1 && (System.Convert.ToInt32(parameter) == 1))
                return true;

            //else if (value != DBNull.Value && System.Convert.ToInt32(value) == 0 && (System.Convert.ToInt32(parameter) == 0))
            //{
            //    return true;
            //}

            else if (value == DBNull.Value && System.Convert.ToInt32(parameter) == 0)
            {
                return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.Equals(true) ? parameter : Binding.DoNothing;
        }

    }

    public class FormModeToReadOnlyBoolean : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((Modes)(value) == Modes.New)
            {
                return false;
            }

            if ((Modes)(value) == Modes.Edit && (parameter.ToString().Trim() != "N"))
            {
                return false;
            }

            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //if ((System.Convert.ToBoolean(value) && System.Convert.ToString(parameter).Equals("Y"))
            //    || (!System.Convert.ToBoolean(value) && System.Convert.ToString(parameter).Equals("N")))
            //{
            //    return 'Y';
            //}
            //else
            //{
            //    return 'N';
            //}
            return false;
        }
    }

    public class TxnFormModeToBoolean : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if ((Modes)(values[0]) == Modes.New && (parameter.ToString().Trim() != "N")) 
            {
                return true;
            }

            if (((Modes)(values[0]) == Modes.Edit || (Modes)(values[0]) == Modes.View) && string.IsNullOrEmpty((string)(values[1])) && (parameter.ToString().Trim() == "N"))
            {
                return true;
            }

            if (((Modes)(values[0]) == Modes.Edit || (Modes)(values[0]) == Modes.View) && !string.IsNullOrEmpty((string)(values[1])) && (parameter.ToString().Trim() != "N"))
            {
                return true;
            }

            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class TxnFormModeExRangeToBoolean : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (((Modes)(values[0]) == Modes.New || (Modes)(values[0]) == Modes.Edit) && string.IsNullOrEmpty((string)(values[1])) && ((string)(values[1])).Trim() != GlobalVariables.BaseCurrency)
            {
                return true;
            }

            return false;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
